﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers
{
    [RouteArea("admin")]
    [RoutePrefix("notice")]
    public class NoticeController : AdminController
    {
        private INoticeService _iNoticeService;

        public NoticeController(INoticeService iNoticeService)
        {
            _iNoticeService = iNoticeService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult Gets(NoticeQuery query)
        {
            var model = _iNoticeService.GetNotices(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        public JsonResult Get(long id)
        {
            notice_dto_ex model = new notice_dto_ex() { status = ENoticeStatus.Unpublished, type= ENoticeType.APP, release_time = DateTime.Now };
            if (id > 0)
                _iNoticeService.GetNotice(id).ToCopyProperty(model);
            model.status_value = ((int)model.status).ToString();//ng-select 需要绑定字符串类型
            model.type_value = ((int)model.type).ToString();//ng-select 需要绑定字符串类型
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult Add(notice_dto_ex dto)
        {
            dto.status = (ENoticeStatus)int.Parse(dto.status_value);
            dto.type = (ENoticeType)int.Parse(dto.type_value);
            var result = _iNoticeService.AddNotice(dto);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);

        }

        [HttpPost]
        public JsonResult Update(notice_dto_ex dto)
        {
            dto.status = (ENoticeStatus)int.Parse(dto.status_value);
            dto.type = (ENoticeType)int.Parse(dto.type_value);
            var result = _iNoticeService.UpdateNotice(dto);
            return Json(ResultToJson.Json(result > 0 ? " 保存成功" : "未做任何修改", result), JsonRequestBehavior.AllowGet);

        }

        [HttpDelete]
        public JsonResult Delete(long id)
        {
            var result = _iNoticeService.DeleteNotice(id);
            return Json(ResultToJson.Json(result > 0 ? " 删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public JsonResult publish(long id)
        {
            var result = _iNoticeService.PublishNotice(id);
            return Json(ResultToJson.Json(result > 0 ? " 发布成功" : "发布失败", result));
        }
    }
}